package com.bestcem.xm.member.controller;

import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.controller.vo.SourceCardVO;
import com.bestcem.xm.member.grpc.service.param.ListSourceCardsParam;
import com.bestcem.xm.member.grpc.service.param.PageParam;
import com.bestcem.xm.member.service.SourceCardService;
import com.bestcem.xm.member.service.dto.PageDTO;
import com.bestcem.xm.member.service.dto.SourceCardDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 来源名片 控制层
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/8/18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/member")
public class SourceCardController {
    /**
     * 来源名片服务
     *
     */
    @Autowired
    private SourceCardService sourceCardService;

    /**
     * 获取来源名片列表
     *
     * @param mid
     * @param page
     * @param rowsPerPage
     * @return
     */
    @GetMapping("/mcards")
    public XmResultJson<PageDTO<SourceCardVO>> listSourceCards(
            @RequestParam(value = "mid", required = false)
            @NotBlank(message = "联系人标识不能为空")
            @Pattern(regexp = RegTypes.OBJECT_ID, message = "联系人标识格式错误") String mid,
            @RequestParam(value = "page", required = false)
            @NotNull(message = "当前页不能为空")
            @Min(value = 1, message = "当前页不能小于1") Integer page,
            @RequestParam("rowsPerPage")
            @NotNull(message = "分页大小不能为空")
            @Min(value = 1, message = "分页大小不能小于1")
            @Max(value = 1000, message = "分页大小不能大于1000") Integer rowsPerPage,
            HttpServletRequest request) {

        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        if (log.isInfoEnabled()) {
            log.info("orgId: {}", orgId);
            log.info("userId: {}", userId);
        }
        ListSourceCardsParam param = new ListSourceCardsParam();
        param.setMemberId(mid);
        param.setOrgId(orgId);
        PageParam pageParam = new PageParam();
        pageParam.setSize(rowsPerPage);
        pageParam.setPage(page);
        param.setPage(pageParam);
        ServiceResult<PageDTO<SourceCardDTO>> result = sourceCardService.listSourceCards(param);
        if (!result.isSuccess()) {
            return XmResultJson.fail(-1, result.getMsg());
        }

        // 处理返回结果
        PageDTO<SourceCardDTO> pageDTO = result.getData();
        List<SourceCardVO> sourceCardVOs = this.toSourceCardVOs(pageDTO.getRows(), orgId);
        PageDTO<SourceCardVO> returnPageDTO = new PageDTO<>();
        returnPageDTO.setTotal(pageDTO.getTotal());
        returnPageDTO.setRows(sourceCardVOs);
        return XmResultJson.success(returnPageDTO);
    }

    /**
     * 类型转换
     *
     * @param sourceCardDTOs
     * @param orgId
     * @return
     */
    private List<SourceCardVO> toSourceCardVOs(List<SourceCardDTO> sourceCardDTOs, String orgId) {
        if (CollectionUtils.isEmpty(sourceCardDTOs)) {
            return Collections.emptyList();
        }

        return sourceCardDTOs.stream().map(sourceCardDTO -> this.totoSourceCardVO(sourceCardDTO, orgId))
                .collect(Collectors.toList());
    }

    /**
     * 类型转换
     *
     * @param sourceCardDTO
     * @param orgId
     * @return
     */
    private SourceCardVO totoSourceCardVO(SourceCardDTO sourceCardDTO, String orgId) {
        SourceCardVO sourceCardVO = new SourceCardVO();
        if (sourceCardDTO == null) {
            return sourceCardVO;
        }

        sourceCardVO.setMid(sourceCardDTO.getMemberId());
        sourceCardVO.setSerial(sourceCardDTO.getNo());
        sourceCardVO.setCreateTime(sourceCardDTO.getCreateTime());
        sourceCardVO.setCreateTime(sourceCardDTO.getUpdateTime());
        sourceCardVO.setSource(sourceCardDTO.getSource());
        sourceCardVO.setData(sourceCardDTO.getData());
        sourceCardVO.setMType(sourceCardDTO.getMType());
        sourceCardVO.setOrgId(orgId);
        return sourceCardVO;
    }
}
